﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Microsoft.ApplicationBlocks.Data;

namespace KP_CommonLibrary.DesignTime
{
    public partial class UC_ChoiceField : UserControl
    {
        public IWindowsFormsEditorService EditorService;
        public UC_ChoiceField()
        {
            InitializeComponent();
        }
        public string OlderField { get; set; }
        private string _tableName = "";
        private string _SelectedField;
        public string SelectedField
        {
            get
            {
                if (listBox1.SelectedValue != null)
                {
                    if (listBox1.SelectedIndex > 0)
                        _SelectedField = listBox1.SelectedValue.ToString();
                    else
                        _SelectedField = null;
                }
                return _SelectedField;
            }
            set
            {
                _SelectedField = value;
            }
        }
        public bool IsNullable
        {
            get
            {
                if (listBox1.SelectedValue == null)
                    return true;
                else
                {
                    DataRowView view =(DataRowView) listBox1.SelectedItem;
                    return bool.Parse(view["Is_Nullable"].ToString());

                }
            }
        }
        public int MaxLength
        {
            get
            {
                if (listBox1.SelectedValue == null)
                    return -1;
                else
                {
                    DataRowView view = (DataRowView)listBox1.SelectedItem;
                    return int.Parse(view["Max_Length"].ToString());

                }
            }
        }
        public void LoadTableList()
        {
            try
            {
                DataTable tbl = SqlHelper.ExecuteDataTable(DesignUtil.ConnectionString, CommandType.Text,
                     @"Select Name from sys.Tables");
                DataRow row = tbl.NewRow();
                row["Name"] = "(Không chọn)";

                tbl.Rows.InsertAt(row, 0);
                
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DataBind(string tableName)
        {
            _tableName = tableName;
            lbTable.Text =  tableName;
            try
            {
                DataTable tbl = SqlHelper.ExecuteDataTable(DesignUtil.ConnectionString, CommandType.Text,
                     @"select C.Name,C.Max_Length,C.Precision,C.Scale,c.Is_Nullable from [sys].columns C 
                        inner join  [sys].Tables T on C.Object_Id=t.object_ID WHERE T.Name='" + tableName + "'");
                DataRow row = tbl.NewRow();
                row["Name"] = "(Không chọn)";

                tbl.Rows.InsertAt(row, 0);
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Name";
                listBox1.DataSource = tbl;
                if (!string.IsNullOrEmpty(OlderField))
                    listBox1.SelectedValue = OlderField;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            EditorService.CloseDropDown();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DataBind(_tableName);
        }
    }
}
